import {getExtInfo, updateMemberInfo} from "@src/api/member";
import {storeBindingsBehavior} from "mobx-miniprogram-bindings";
import base from "@src/store/base";
import member from "@src/store/member";
import dayjs from "dayjs";

Component({
  behaviors: [storeBindingsBehavior],
  storeBindings: [{
    store: base,
    fields: ['themeColor', 'themeTitleColor'],
  }, {
    store: member,
    fields: ['memberName', 'wxHeadImg', 'sex'],
    actions: ['getBaseInfo']
  }],
  data: {
    inputDialog: {
      visible: false,
      key: '',
      name: '',
      value: '',
    },
    packingArray: ["男", "女"], //付费开卡状态选择
  },
  methods: {
    onLoad() {
    },
    onLogin() {
      this.getCurrentDate();
      this.fetchData()
    },
    onUnload() {
      this.getBaseInfo()
    },
    fetchData() {
      getExtInfo().then(res => {
        if (res.code === 0) {
          this.setData({extendInfo: res.data})
        }
      })
    },
    changeMemberData(data) {
      updateMemberInfo(data).then(res => {
        if (res.code === 0) {
        } else {
          wx.showToast({
            title: "信息修改失败，请重试", icon: "none", duration: 3000
          });
        }
      });
    },
    handleStop() {
      return true;
    },
    handleClickInput(e) {
      let {key, name} = e.currentTarget.dataset
      let value = ""
      if (key === "name") {
        value = this.data.memberName;
      } else {
        value = this.data.extendInfo[key];
      }
      this.setData({inputDialog: {visible: true, key, name, value}})
    },
    handleInput(e) {
      let inputDialog = this.data.inputDialog;
      inputDialog.value = e.detail.value;
      this.setData({inputDialog})
    },
    handleClearInput() {
      let inputDialog = this.data.inputDialog;
      inputDialog.value = "";
      this.setData({inputDialog})
    },
    handleCloseInput() {
      let inputDialog = this.data.inputDialog;
      inputDialog.visible = false;
      this.setData({inputDialog})
    },
    handleConfirmInput() {
      let {key, name, value} = this.data.inputDialog;
      if (this.checkInput(key, name, value)) {
        if (key === 'name') {
          this.setData({memberName: value, inputDialog: null})
        } else {
          let extendInfo = this.data.extendInfo
          extendInfo[key] = value
          this.setData({extendInfo: extendInfo, inputDialog: null})
        }
        let data = {};
        data[key] = value;
        this.changeMemberData(data);
      }
    },
    checkInput(key, name, value) {
      if (value == null || value === "") {
        wx.showToast({title: `${name}字段不能为空`, icon: "none", duration: 3000});
        return false;
      }
      if (key === "email" && !/^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/.test(value)) {
        wx.showToast({title: `${name}字段输入有误`, icon: "none", duration: 3000});
        return false;
      }
      if (key === "idCard" && !/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(value)) {
        wx.showToast({title: `${name}字段输入有误`, icon: "none", duration: 3000});
        return false;
      }
      return true;
    },
    handleGetLocation() {
      wx.chooseLocation({
        success: res => {
          let extendInfo = this.data.extendInfo;
          extendInfo.address = res.address;
          this.setData({extendInfo})
          this.changeMemberData({address: res.address});
        }, fail: e => {
          if (e.errMsg.indexOf("auth deny") !== -1) {
            wx.showModal({
              title: "位置授权提示",
              content: "由于您未授权请按以下步骤进行位置授权 \n 1.点击右上角（三个点的位置） - 2.选择关于 - 3.再点击右上角 - 4.选择设置- 5.开启相应权限开关",
              showCancel: false,
              success(res) {
                if (res.confirm) {
                }
              }
            });
          }
        }
      });
    },
    //性别选择
    handlePickerChange(e) {
      console.info(e)
      let sex = Number(e.detail.value) === 1 ? 0 : 1;
      this.setData({sex: sex})
      this.changeMemberData({sex: sex});
    },
    //获取选择的时间
    handleDateChangeStart(event) {
      let extendInfo = this.data.extendInfo;
      extendInfo.birthday = event.detail.value;
      this.setData({extendInfo: extendInfo})
      this.changeMemberData({birthday: event.detail.value});
    },
    //获取当前时间
    getCurrentDate() {
      let startDate = dayjs(new Date().setFullYear(new Date().getFullYear() - 120)).format("YYYY-MM-DD");
      let pickerStart = dayjs(new Date().setFullYear(new Date().getFullYear() - 20)).format("YYYY-MM-DD");
      let endDate = dayjs(new Date().setFullYear(new Date().getFullYear())).format("YYYY-MM-DD");
      this.setData({startDate, pickerStart, endDate})
    }
  }
})
